import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/provider_index_setting.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/domain/test/submission_file_upload.dart';
import 'package:flutter_syt/domain/widgets/flow_item.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:provider/provider.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class WayStationNewsListPage extends StatefulWidget {
  @override
  _WayStationNewsListPageState createState() => _WayStationNewsListPageState();
}

class _WayStationNewsListPageState extends State<WayStationNewsListPage>
    with AutomaticKeepAliveClientMixin {
  var _needShowVolunteer = false;

  @override
  void initState() {
    void asyncFunc() async {
      try {
        _needShowVolunteer = await API.shared.user.volunteerShow();
        if (mounted) {
          setState(() {});
        }
      } catch (e) {}
    }

    if (UserInfo.me.isLogin) {
      asyncFunc();
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: SafeArea(
          child: Stack(
        children: [
          NormalTableView<FlowItemEntity>(
            itemBuilder: (context, item) {
              return GestureDetector(
                  child: FlowItem(item),
                  onTap: ClickUtils.debounce(
                    () {
                      debugPrint("${item.linkUrl}");
                      FocusScope.of(context).requestFocus(FocusNode());
                      RouteManager.push(context, item.linkUrl);
                    },
                  ));
            },
            getData: (pageIdx) async {
              var list = await API.shared.volunteer.newsList(pageIdx);
              return list
                  .map((e) => FlowItemEntity.create(
                      e.title, e.publish_time, e.thumb, e.linkUrl,
                      labelText: e.tag))
                  .toList();
            },
          ),
          Consumer<ProviderIndexSetting>(
            builder: (context, indexProvider, child) {
              return Visibility(
                  visible: _needShowVolunteer &&
                      indexProvider.indexSetting.volunteerContribution == 1,
                  child: Positioned(
                    child: GestureDetector(
                        onTap: ClickUtils.debounce(() {
                          Navigator.of(context).push(MaterialPageRoute(
                              builder: (context) =>
                                  SubmissionFileUploadPage()));
                        }),
                        child: Icon(
                          Icons.add_circle_sharp,
                          size: 44,
                          color: Colors.yellow,
                        )),
                    bottom: 20,
                    right: 20,
                  ));
            },
          )
          // if (_needShowVolunteer)
          ,
        ],
      )),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
